//
//  LJFHeadView.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/15.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFHeadView.h"
#import "LJFHttpManager.h"
#import "UIImageView+AFNetworking.h"
#import "Masonry.h"


@implementation LJFHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _dataArr = [[NSMutableArray alloc] init];
        
        [self loadDataFromNet];
        
    }
    return self;
}

#pragma mark - 从网络加载表头数据

- (void)loadDataFromNet
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_HomeScorll AndParameterStr:URL_HomeScorll_parameter success:^(id responseObj)
    {
        NSArray *arr = responseObj[@"data"][@"player"];
        
        [_dataArr addObjectsFromArray:arr];
        
        [self loadScroll];
        
    } failure:^(NSError *error)
    {
        NSLog(@"error == %@",error);
    }];
}

#pragma mark - 加载数据完成,将数据显示在表头

- (void)loadScroll
{
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    
    _ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, 150)];
    _ScrollView.contentSize = CGSizeMake(width*3, 0);
    
    _ScrollView.delegate = self;
    
    _ScrollView.pagingEnabled = YES;
    
    [self addSubview:_ScrollView];
    
    for (int i = 0; i < _dataArr.count; i++)
    {
        UIImageView *imgview=[[UIImageView alloc] initWithFrame:CGRectMake(i*width, 0, width, 150)];
        imgview.userInteractionEnabled = YES;
        imgview.tag = 185+i;
        
        NSDictionary *dict = _dataArr[i];
        [imgview setImageWithURL:[NSURL URLWithString:dict[@"photo"][@"small"]] placeholderImage:[UIImage imageNamed:@"default_image.png"]];
        
        [_ScrollView addSubview:imgview];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        
        [imgview addGestureRecognizer:tap];
        
    }
    
    _page=[[UIPageControl alloc] initWithFrame:CGRectMake((width-100)/2.0, 120, 100, 30)];
    
    _page.numberOfPages=_dataArr.count;
    
    _page.pageIndicatorTintColor=[UIColor whiteColor];
    _page.currentPageIndicatorTintColor=[UIColor greenColor];
    
    [self addSubview:_page];

    [self Starttime];
}

- (void)tap:(UITapGestureRecognizer *)tap
{
    UIImageView *imageView = (UIImageView *)tap.view;
    
    NSInteger tag = imageView.tag - 185;
    
    NSDictionary *dict = _dataArr[tag];
    
    [_mydelegate headViewImageClick:self WithUrl:dict[@"url"]];
    
}

- (void)Starttime
{
    _time = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(go) userInfo:nil repeats:YES];
    
    NSRunLoop *loop=[NSRunLoop currentRunLoop];
    
    [loop addTimer:_time forMode:NSRunLoopCommonModes];
}

- (void)go
{
    _index ++;
    
    if (_index >= _dataArr.count)
    {
        _index = 0;
    }
    
    [_ScrollView setContentOffset:CGPointMake(self.frame.size.width*_index, 0) animated:YES];
    _page.currentPage = _index;
}

- (void)stopTime
{
    if ([_time isValid])
    {
        [_time invalidate];
        _time = nil;
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTime];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self Starttime];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/



@end
